package com.qianfeng.zhangwei.sybl.utils;

import android.text.TextUtils;
import android.widget.BaseAdapter;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.gc.flashview.FlashView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.qianfeng.zhangwei.sybl.bean.Banner;
import com.qianfeng.zhangwei.sybl.bean.Entity;
import com.qianfeng.zhangwei.sybl.config.AppConfig;
import com.qianfeng.zhangwei.sybl.config.UrlContants;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;

/**
 * @说 明：
 * @项目名称：Sz1504
 * @包 名： com.qianfeng.zhangwei.sybl.utils
 * @类 名： AsyncHttpUtils
 * @创 建人： zhangwei
 * @创建时间：2015-07-28 21:53
 * @版 本：v1.0
 * @修 改人：
 * @修改时间：
 * @修改备注：
 */
public class AsyncHttpUtils {
    private static AsyncHttpUtils asyncHttpUtils;
    private AsyncHttpClient asyncHttpClient;

    private AsyncHttpUtils() {
        asyncHttpClient = new AsyncHttpClient();
    }

    public static AsyncHttpUtils getInstance() {
        if (asyncHttpUtils == null) {
            asyncHttpUtils = new AsyncHttpUtils();
        }
        return asyncHttpUtils;
    }

    public <T> RequestHandle get(String url, final TypeReference<Entity> reference, final PullToRefreshListView ptrListView, final List<T> list, final BaseAdapter adapter) {
        RequestHandle handle = asyncHttpClient.get(url, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                ptrListView.onRefreshComplete();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                /**
                 *
                 */
                Entity entity = JSONObject.parseObject(responseString, reference);
                if (entity != null && !TextUtils.isEmpty(entity.getState()) && entity.getState().equals(AppConfig.LOAD_DATA_SUCCESS)) {
                    list.addAll(entity.getInfo());
                    adapter.notifyDataSetChanged();
                }

            }
        });
        return handle;
    }

    /**
     * @param <T>
     * @param url
     * @param reference
     * @param requestParams
     * @param ptrListView
     * @param list
     * @param adapter
     * @param number
     */
    public <T> RequestHandle post(String url, final TypeReference<Entity<T>> reference, RequestParams requestParams, final PullToRefreshListView ptrListView, final List<T> list, final BaseAdapter adapter, final int number) {
        RequestHandle handle = asyncHttpClient.post(url, requestParams, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                ptrListView.onRefreshComplete();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                Entity entity = JSONObject.parseObject(responseString, reference);
                if (entity != null && !TextUtils.isEmpty(entity.getState()) && entity.getState().equals(AppConfig.LOAD_DATA_SUCCESS)) {
                    if (number == 1 && list.size() > 0) {
                        list.clear();
                    }

                }
                list.addAll(entity.getInfo());
                adapter.notifyDataSetChanged();
                ptrListView.onRefreshComplete();
                if (number >= entity.getPage().getPage_total()) {
                    ptrListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
                }
            }
        });
        return handle;
    }


    public <T> RequestHandle post(String url, final TypeReference<Entity<T>> reference, RequestParams requestParams, final List<T> list, final BaseAdapter adapter) {
        RequestHandle handle = asyncHttpClient.post(url, requestParams, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                Entity entity = JSONObject.parseObject(responseString, reference);
                if (entity != null && !TextUtils.isEmpty(entity.getState()) && entity.getState().equals(AppConfig.LOAD_DATA_SUCCESS)) {

                    list.addAll(entity.getInfo());
                    adapter.notifyDataSetChanged();
                }
            }
        });
        return handle;
    }


    public <T> RequestHandle postBannerData(RequestParams requestParams, final FlashView flashView, final List<Banner> banners) {
        RequestHandle handle = asyncHttpClient.post(UrlContants.URL_COMMON_BANNER, requestParams, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                TypeReference<Entity<Banner>> typeReference = new TypeReference<Entity<Banner>>() {
                };
                Entity<Banner> entity = JSONObject.parseObject(responseString, typeReference);
                if (entity != null && !TextUtils.isEmpty(entity.getState()) && entity.getState().equals(AppConfig.LOAD_DATA_SUCCESS)) {
                    banners.clear();
                    banners.addAll(entity.getInfo());
                    ArrayList<String> imageUrls = new ArrayList<String>();
                    for (Banner banner : entity.getInfo()) {
                        imageUrls.add(banner.getBimg());
                    }
                    flashView.setImageUris(imageUrls);
                }
            }
        });
        return handle;
    }


}
